using UnityEngine;
using System.Collections;

public class CellGrid : MonoBehaviour, IPlayLevelElements {
	
	public bool hasTexture = false;	
	
	public int gridPosX;
	public int gridPosY;
	
	void Start()
	{
		renderer.enabled = true;
		collider.enabled = true;
		collider.isTrigger = true;
	}

	void OnMouseDown()
	{	
		LevelAssetManager.CellComponent cm = LevelAssetManager.Instance.ChosenCell;
		
		if(cm == null)
			return;
		
		if(LevelColliderMaker.Instance.isIgnored(cm.name))
		{
			hasTexture = true;
			renderer.material = LevelAssetManager.Instance.getAsset<Material>(KEYTYPE.MATERIAL, "CellMat");
			renderer.material.mainTexture = cm.image;
			gameObject.name = cm.name;
			LevelColliderMaker.Instance.deleteCell(this);
			return;
		}
		
		//Delete cell area
		if(cm.name == "zxDelete")
		{
			hasTexture = false;
			renderer.material = LevelAssetManager.Instance.getAsset<Material>(KEYTYPE.MATERIAL, "Wireframe");
			gameObject.name = "Cell(Clone)";
			LevelColliderMaker.Instance.deleteCell(this);
			return;
		}
		
		//Add cell Area
		hasTexture = true;
		renderer.material = LevelAssetManager.Instance.getAsset<Material>(KEYTYPE.MATERIAL, "CellMat");
		renderer.material.mainTexture = cm.image;
		gameObject.name = cm.name;
		
		//Add collider
		LevelColliderMaker.Instance.addCell(this);
	}
	
	public void Strip_Add_Components()
	{
		if(!hasTexture)
		{
			Destroy(gameObject);
		}
		
		else
		{
			Destroy(this.collider);
			Destroy(this);
		}
	}
	public void setHasTexture(Texture2D image, Material mat)
	{
		hasTexture = true;
		renderer.material = mat; 
		gameObject.renderer.material.mainTexture = image;
	}
	public bool HasTexture
	{
		get{ return hasTexture;}
	}
}
